package com.caine.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;

/**
 * @Author: CaineZhu
 * @Description: file  A to B
 * @Date: Created in 10:05 2020/11/3
 * @Modified By:
 */
public class FileChannel02 {
    public static void main(String[] args) throws Exception {
        String filePath = "D:\\file.txt";
        FileInputStream fileInputStream = new FileInputStream(filePath);
        FileChannel channel01 = fileInputStream.getChannel();

        FileOutputStream fileOutputStream = new FileOutputStream("D:\\file02.txt");
        FileChannel channel02 = fileOutputStream.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        while (true) {
            byteBuffer.clear();
            int read = channel01.read(byteBuffer);
            if (read == -1) {
                break;
            }
            byteBuffer.flip();
            channel02.write(byteBuffer);
        }
        channel01.close();
        channel02.close();


    }
}
